/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年9月3日 下午8:28:40
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.woniu.shiro.domain.User;
import com.woniu.shiro.service.IUserService;

/**
 * @author Administrator
 *
 */
public class ShiroRealm extends AuthorizingRealm{
	@Autowired
	 IUserService us;
	//用于授权
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}
	//用于认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken uptoken = (UsernamePasswordToken) token;
		//获取前端传来的用户名
		String username = uptoken.getUsername();
		//获取从数据库查到的数据
		User user = us.login(username);
		if(user == null) {
			throw new UnknownAccountException("账户不存在");
		}
		System.out.println(String.valueOf(uptoken.getPassword()));
		System.out.println(uptoken.getUsername());
		System.out.println(user.getUname());
		System.out.println(user.getUpass());
		//char[] upass = uptoken.getPassword();
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUname(),user.getUpass(),this.getName());
		return info;
	}
	
}
